//
//  StartViewController.m
//  享动
//
//  Created by 沈浩hh on 16/4/8.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "StartViewController.h"
#import "RootTabBarViewController.h"

@interface StartViewController ()

@property(nonatomic, retain)UIButton *startButton;
@property(nonatomic, retain)NSTimer *timer;

@property (nonatomic, assign) NSInteger time;
@end

@implementation StartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _time = 4;
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
    imageView.image = [UIImage imageNamed:@"runStart"];
    
    [self.view addSubview:imageView];
    
    
    _startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _startButton.frame = CGRectMake(KWIDTH - 100, KHEIGHT - 50, 80, 30);
    [_startButton setTitle:@"跳过  3" forState:UIControlStateNormal];
    
    // 设置边框
    [_startButton.layer setBorderWidth:1];
    [_startButton.layer setCornerRadius:5];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 1, 1, 1 });
    [_startButton.layer setBorderColor:colorref];

    [_startButton addTarget:self action:@selector(rootView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_startButton];
    
     _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
}

- (void)timeAction
{
    _time--;
    [_startButton setTitle:[NSString stringWithFormat:@"跳过  %ld", _time - 1] forState:UIControlStateNormal];
    if (_time == 0) {
        [_timer invalidate];
        UIWindow *window = [UIApplication sharedApplication].delegate.window;
        [UIView animateWithDuration:1 animations:^{
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
        } completion:nil];
        RootTabBarViewController *rootTabBarVc = [[RootTabBarViewController alloc]init];
        window.rootViewController = rootTabBarVc;
    }
}

- (void)rootView
{
    [_timer invalidate];
     UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [UIView animateWithDuration:1 animations:^{
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
    } completion:nil];
    RootTabBarViewController *rootTabBarVc = [[RootTabBarViewController alloc]init];
    window.rootViewController = rootTabBarVc;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
